#include "identity_Store_Test.h"

#define uchar unsigned char

idata unsigned char ID_NUM_char[ID_COUNT][4] = {0};//储存ID卡号-20个卡
idata unsigned char ID_authorize[3] = {0};//24个卡授权-只用到20个
idata unsigned char finger_authorize_char[3] = {0};//24个指纹授权
idata unsigned char ID_NUM_COUNT = 0;//ID卡的空白位置

//===================外部不可见==================

//判断是否有匹配的ID卡信息。返回ID号或者0xFF
char ID_serialtoNum(unsigned char *id_num);
//根据ID卡序列号判断授权信息
char ID_numtoAuthoriza(char id_num);
//写入新的ID卡信息
void ID_updateSerial(unsigned char *new_id);
//写入新的ID卡认证信息
void ID_updateAuthoria(char ID,char flag);
//获取当前ID卡数量-空白内存位置
void SetIDNum();

//根据指纹序列号判断认证信息
char finger_numtoAuthorize(char finger_num);
//写入新的指纹认证信息
void finger_updateAuthoria(char ID,char flag);

//eeprom载入内存
void ID_eeprom_to_ram();
void ID_authorize_eeprom_to_ram();
void Friger_authorize_eeprom_to_ram();

//=======================外部可见=======================
//eeprom载入内存-ID信息、ID卡认证信息、指纹认证信息-初始化
void identity_Store_Init(){
ID_eeprom_to_ram();//id卡信息载入内存
ID_authorize_eeprom_to_ram();//id卡认证信息载入内存
Friger_authorize_eeprom_to_ram();//指纹认证信息载入内存
}
//添加ID卡，添加成功返回0x01、添加失败返回0x00
char add_ID(unsigned char* id){
	if((unsigned char)ID_serialtoNum(id) != 0xFF)// 检测是否有重复身份卡
		return 0x00;//存在重复指纹返回0x00
		SetIDNum();
	if(ID_NUM_COUNT == ID_COUNT)//卡片数量达到上限
		return 0x02;//卡片数量达到上线返回0x02
	ID_updateSerial(id);//录入新的ID卡信息
	return 0x01;//正确返回0x01
}
//检测ID卡、检测到且授权返回0x01、否则返回0x00
char check_ID(unsigned char *id){
	return ID_numtoAuthoriza(ID_serialtoNum(id));
}
//授权ID卡
void authorize_ID(char ID,char flag){
	ID_updateAuthoria(ID, flag);
}
//检测指纹
char check_finger(char count){
	return finger_numtoAuthorize(count);
}
//授权指纹
void authorize_finger(char ID,char flag){
	finger_updateAuthoria(ID, flag);
}

//======================外部不可见====================
//判断是否有匹配的ID卡信息返回序列号
char ID_serialtoNum(unsigned char *id_num){
	idata char i=0, j=0;
	
	for(i=0; i<ID_COUNT; i++){
		
		if(ID_NUM_char[i][0]==id_num[0] &&\
			 ID_NUM_char[i][1]==id_num[1] &&\
	     ID_NUM_char[i][2]==id_num[2] &&\
		   ID_NUM_char[i][3]==id_num[3]){
			return i;
		}
	}
		return 0xFF;
}
//根据ID卡序列号判断授权信息
char ID_numtoAuthoriza(char id_num){
	
	if(id_num <= 7){//0~7
		if(ID_authorize[0] & 1<<(7-id_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else if(id_num <= 15){//8~15
	id_num -= 8;
		if(ID_authorize[1] & 1<<(7-id_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else if(id_num <= 19){//16~19
	id_num -= 16;
		if(ID_authorize[2] & 1<<(7-id_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else{//错误的ID卡序列号
			return 0;
	}
}
//写入新的ID卡认证信息
void ID_updateAuthoria(char ID,char flag){

	if(ID <= 7){//0~7
		//SendData(ID);
		ID_authorize[0] = flag ? ID_authorize[0]|(1<<(7-ID)):ID_authorize[0]&~(1<<(7-ID));
	}
	else if(ID <= 15){//8~15
	ID -= 8;
		ID_authorize[1] = flag ? ID_authorize[1]|(1<<(7-ID)):ID_authorize[1]&~(1<<(7-ID));
	}
	else{//16~19
	ID -=16;	
		ID_authorize[2] = flag ? ID_authorize[2]|(1<<(7-ID)):ID_authorize[2]&~(1<<(7-ID));	
	}

	IapEraseSector(0x200);       //擦除第二扇区-写入
	IapProgramByte(0x200+0, ID_authorize[0]);
	IapProgramByte(0x200+1, ID_authorize[1]);
	IapProgramByte(0x200+2, ID_authorize[2]);
}
//清空ID卡和认证信息-ram和eeprom
void clear_identity(){
	IapEraseSector(0x000);//清空第1个扇区
	IapEraseSector(0x200);//清空第2个扇区
	IapEraseSector(0x400);//清空第3个扇区	
	//刷新内存
	identity_Store_Init();

}
//写入新的ID卡信息到eeprom
void ID_updateSerial(unsigned char *new_id){
	idata char i=0, j=0;
	SetIDNum();//统计当前空白的ID卡位置
	for(i=0; i<4; i++){
		ID_NUM_char[ID_NUM_COUNT][i] = new_id[i];
//		SendData(ID_NUM_char[ID_NUM_COUNT][i]);
	}
	IapEraseSector();
	for(i=0; i<ID_NUM_COUNT+1; i++){//Ram写入eeprom
		for(j=0; j<4; j++)
			IapProgramByte(0x000 + i*4 + j, ID_NUM_char[i][j]);
	}
}
//根据指纹ID号判断认证信息
char finger_numtoAuthorize(char finger_num){
	if(finger_num <= 7){//0~7
		if(finger_authorize_char[0] & 1<<(7-finger_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else if(finger_num <= 15){//8~15
	finger_num -= 8;
		if(finger_authorize_char[1] & 1<<(7-finger_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else if(finger_num <= 19){//16~19
	finger_num -= 16;
		if(finger_authorize_char[2] & 1<<(7-finger_num)){
			return 0x01;
		}else{
			return 0x00;
		}
	}
	else{//错误的指纹序列号
			return 0;
	}
}

//写入新的指纹认证信息
void finger_updateAuthoria(char ID,char flag){
	if(ID <= 7){//0~7
		finger_authorize_char[0] = flag ? finger_authorize_char[0]|(1<<(7-ID)):finger_authorize_char[0]&~(1<<(7-ID));
	}
	else if(ID <= 15){//8~15
	ID -= 8;
		finger_authorize_char[1] = flag ? finger_authorize_char[1]|(1<<(7-ID)):finger_authorize_char[1]&~(1<<(7-ID));
	}
	else{//16~19
	ID -=16;	
		finger_authorize_char[2] = flag ? finger_authorize_char[2]|(1<<(7-ID)):finger_authorize_char[2]&~(1<<(7-ID));	
	}	
	IapEraseSector(0x400);       //擦除第二扇区-写入
	IapProgramByte(0x400+0, finger_authorize_char[0]);
	IapProgramByte(0x400+1, finger_authorize_char[1]);
	IapProgramByte(0x400+2, finger_authorize_char[2]);
}
//----------------------外部不可见-----------------------
void SetIDNum(){//统计当前空白的ID卡位置
	idata char i=0;
	ID_NUM_COUNT=0;
	for(i=0; i<ID_COUNT; i++){
	if((uchar)ID_NUM_char[i][0] != 0xFF || \
		 (uchar)ID_NUM_char[i][1] != 0xFF || \
	   (uchar)ID_NUM_char[i][2] != 0xFF || \
		 (uchar)ID_NUM_char[i][3] != 0xFF)
	ID_NUM_COUNT++;
	else
	return ;
	}
}

void ID_eeprom_to_ram(){
	idata char i=0, j=0;
	for(i=0; i<ID_COUNT; i++){//载入ID卡号
		for(j=0; j<4; j++)
			ID_NUM_char[i][j] = 	IapReadByte(0x000 + i*4 + j);
	}
}
void ID_authorize_eeprom_to_ram(){
	idata char i=0;
	for(i=0; i<3; i++)//载入卡片授权
		ID_authorize[i] = IapReadByte(0x200 + i);
}
void Friger_authorize_eeprom_to_ram(){
	idata char i=0;
	for(i=0; i<3; i++)//载入指纹授权 
		finger_authorize_char[i] = IapReadByte(0x400 + i);
}
